using Domain.Entities;
using Domain.Misc;
using NHibernate;

namespace Domain.Services
{
    public static class SettingsService
    {
        private const byte SettingsId = 1;

        /*****************************************************************************************
        * Getters
        * ***************************************************************************************/
        public static Settings Get()
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            return session.Get<Settings>(SettingsId);
        }

        /*****************************************************************************************
        * Adders
        * ***************************************************************************************/
        /*****************************************************************************************
        * Updaters
        * ***************************************************************************************/
        public static void UpdateGlobalSettings
        (
            string boardName,
            bool allowNewRegistrations,
            bool accountActivationByEmail,
            bool accountActivationByAdmin
        )
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                Settings settings = BoardSettingsManager.Instance.Settings;
                settings.BoardName = boardName;
                settings.AllowNewRegistrations = allowNewRegistrations;
                settings.AccountActivationByEmail = accountActivationByEmail;
                settings.AccountActivationByAdmin = accountActivationByAdmin;
                session.SaveOrUpdate(settings);

                transaction.Commit();
            }
        }

        public static void UpdateEmailSettings
        (
            string fromAddress,
            string fromDisplayName,
            string smtpHost,
            int smtpPort,
            string smtpUsername,
            string smtpPassword,
            bool enableSsl
        )
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                Settings settings = BoardSettingsManager.Instance.Settings;
                settings.EmailFromAddress = fromAddress;
                settings.EmailFromDisplayName = fromDisplayName;
                settings.EmailSmtpHost = smtpHost;
                settings.EmailSmtpPort = smtpPort;
                settings.EmailSmtpUsername = smtpUsername;
                settings.EmailSmtpPassword = smtpPassword;
                settings.EmailEnableSsl = enableSsl;
                session.SaveOrUpdate(settings);

                transaction.Commit();
            }
        }

        /*****************************************************************************************
        * Deleters
        * ***************************************************************************************/
    }
}
